package thinkinginjava.concurrency;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class FutureRenderer {
	private final ExecutorService exec = Executors.newCachedThreadPool();
	
	void renderPage(CharSequence source) {
		final List<ImageInfo> imageInfos = scanImageInfos(source);
		
		Callable<List<ImageData>> task = new Callable<List<ImageData>>() {

			@Override
			public List<ImageData> call() throws Exception {
				List<ImageData> results = new ArrayList<ImageData>();
				for(ImageInfo info : imageInfos) {
					results.add(info.downloadImage());
				}
				return results;
			}
		};
		
		Future<List<ImageData>> future = exec.submit(task);
		// renderText(source);
		try {
			List<ImageData> imageData = future.get();
			for(ImageData image : imageData) {
				// renderImage(image);
			}
		} catch (InterruptedException e) {
			Thread.currentThread().interrupt();
			future.cancel(true);
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private List<ImageInfo> scanImageInfos(CharSequence source) {
		// TODO Auto-generated method stub
		return null;
	}
}

class ImageInfo {
	public ImageData downloadImage() {
		return new ImageData();
	}
}

class ImageData {
	
}
